package com.jintoufs.web.action.travelagency;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.travelagency.TravelAgency;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.travelagency.TravelAgencyService;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.exception.ServiceException;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zhenghq
 * @Despription : 假期管理
 * @Date 2018/1/25 15:32
 * @Modified by
 */
@Controller
@Scope("prototype")
@RequestMapping("/travelagency")
public class TravelAgencyAction {

    @Autowired
    TravelAgencyService travelAgencyService;

    @RequestMapping("list")
    String list(){
       return "travelagency/list";
    }
    /**
     * 查询线路信息
     */
    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = StringUtils.isEmpty(filters)?new QueryFilters()
                :JSON.parseObject(filters, QueryFilters.class);
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map> list = travelAgencyService.getByFilter(filters_);
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getList());
    }
    @RequestMapping("input")
    String input(Model model, String id){
        if(StringUtils.isNotEmpty(id)){
            model.addAttribute("ta",travelAgencyService.getTravelAgencyById(id));
            model.addAttribute("isEdit",true);
        }else {
            model.addAttribute("isEdit",false);
        }
        return "travelagency/input";
    }
    @RequestMapping("detail")
    String detail(Model model,String id){
        model.addAttribute("ta",travelAgencyService.getTravelAgencyById(id));
        return "travelagency/detail";
    }

    @RequestMapping("save")
    @ResponseBody
    @SystemControllerLog(description = "保存假期")
    AjaxMsg save(TravelAgency travelAgency, @CurrentUser Customer customer){
        AjaxMsg msg = new AjaxMsg();
        try {
            travelAgency.setManagerId(customer.getId());
            travelAgencyService.save(travelAgency,customer);
            msg.setCode("200");
            msg.setMessage("保存成功!");
        } catch (ServiceException e) {
            e.printStackTrace();
            msg.setCode("500");
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode("500");
            msg.setMessage("保存失败!");
        }
        return msg;
    }

    @RequestMapping("delete")
    @ResponseBody
    @SystemControllerLog(description = "删除假期")
    AjaxMsg delete(String id){
        AjaxMsg msg = new AjaxMsg();
        try {
            travelAgencyService.deleteTravelAgencyById(id);
            msg.setCode("200");
            msg.setMessage("删除成功");
        } catch (ServiceException e) {
            e.printStackTrace();
            msg.setCode("500");
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode("500");
            msg.setMessage("删除失败");
        }
        return msg;
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) throws Exception {
//        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    /**
     * 查询管理员分页信息
     */
    @RequestMapping("querycustomer")
    @ResponseBody
    public JqgridPage querycustomer(int page, int rows, String sidx, String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = StringUtils.isEmpty(filters)?new QueryFilters()
                : JSON.parseObject(filters, QueryFilters.class);
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Map> list = travelAgencyService.getCustomerInfoByFilter(filters_);
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getList());
    }

    @RequestMapping("querycustomers")
    @ResponseBody
    public AjaxMsg queryCustomers(int page, int rows,String real_name,
                         HttpServletRequest request){
        AjaxMsg msg = new AjaxMsg();
        try {
            PageHelper.startPage(page,rows);
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("real_name",real_name);
            List<Map<String,Object>> list = travelAgencyService.queryCustomerInfo(params);
            PageInfo<List> pageInfo = new PageInfo(list,5);
            msg.setCode("200");
            msg.setDatas(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode("500");
            msg.setMessage("获取管理员分页数据失败");
        }
        return msg;
    }
}
